package liquidfs.ui.web;

import java.io.*;
import java.util.logging.*;

import liquidfs.*;

import com.sun.net.httpserver.*;

class DirectHandler implements HttpHandler {

   private Application app;
   private String filepath;
   
   private static Logger log = Logger.getLogger("liquidfs.ui.web");
   
   public DirectHandler (Application app, String filepath) throws IOException {
      this.app = app;
      this.filepath = filepath;
   }
   
   @Override
   public void handle(HttpExchange http) throws IOException {
      
      log.info("Serving " + http.getHttpContext().getPath());
      
      File file = new File(this.filepath);
      byte[] buffer = new byte[ (int) file.length()]; // Web pages will never be bigger than 2 GB. Lol.
      
      FileInputStream in = new FileInputStream(file);
      in.read(buffer);
      in.close();
      
      // Send the cached file
      http.sendResponseHeaders(200, buffer.length);
      http.getResponseBody().write(buffer);
      http.close();
      
   }
   
}